package cn.weekimwee.firstlineofcodeforkotlin.unit12

import android.os.Bundle
import android.support.design.widget.Snackbar
import android.support.v4.view.GravityCompat
import android.support.v7.widget.GridLayoutManager
import android.view.Menu
import android.view.MenuItem
import cn.weekimwee.firstlineofcodeforkotlin.BaseActivity
import cn.weekimwee.firstlineofcodeforkotlin.R
import kotlinx.android.synthetic.main.activity_unit12.*
import org.jetbrains.anko.startActivity
import org.jetbrains.anko.toast

class Unit12Activity : BaseActivity() {
    private lateinit var adapter:AdapterFruit
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_unit12)
        setSupportActionBar(toolbar)
        val actionbar = supportActionBar
        actionbar?.setDisplayHomeAsUpEnabled(true)
        nav_view.setCheckedItem(R.id.one)
        nav_view.setNavigationItemSelectedListener {
            item ->
            when(item.itemId){
                R.id.one-> toast("one")
                R.id.two-> startActivity<FruitDetailActivity>()
                R.id.three-> toast("three")
                R.id.four-> toast("four")
                R.id.five-> toast("five")
            }
            drawerLayout.closeDrawers()
            true
        }
        fab.setOnClickListener{
            Snackbar.make(it,"this is snackbar",Snackbar.LENGTH_SHORT)
                    .setAction("lalalala",{
                        toast("undo------>")
                    })
                    .show()
        }
        adapter = AdapterFruit(this,null)

        recyclerView.layoutManager = GridLayoutManager(this,2)
        recyclerView.adapter = adapter
        adapter.setNewData(initFruit())
        refresh.setColorSchemeResources(R.color.colorPrimary)
        refresh.setOnRefreshListener {
            Thread({
                Thread.sleep(2000)
                runOnUiThread {
                    adapter.setNewData(initFruit())
                    refresh.isRefreshing = false
                }
            }).start()
        }
        adapter.setOnItemClickListener { _, _, _ ->
            startActivity<FruitDetailActivity>()
        }
    }

    private fun initFruit(): MutableList<Fruit>? {
        return arrayListOf(
                Fruit("苹果","https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1524053203&di=1ab3aee7b5ad5be1d1f5250bc8bd4382&imgtype=jpg&er=1&src=http%3A%2F%2Fwww.photophoto.cn%2Fm6%2F023%2F024%2F0230240027.jpg"),
                Fruit("菠萝","https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2270171933,1269226305&fm=27&gp=0.jpg"),
                Fruit("荔枝","https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1523458516759&di=6ac76b7a1c277ee8c4e804d7c98593f7&imgtype=0&src=http%3A%2F%2Fimg.weixinyidu.com%2F151117%2Fbcc64cef.jpg"),
                Fruit("草莓","https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=1856427000,1788429908&fm=27&gp=0.jpg"),
                Fruit("石榴","https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=949315503,3283317595&fm=27&gp=0.jpg"),
                Fruit("西瓜","https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1523458728133&di=fc2a4f6a288d79050b75537229130350&imgtype=0&src=http%3A%2F%2Fimgsrc.baidu.com%2Fimgad%2Fpic%2Fitem%2Fc83d70cf3bc79f3db0debe22b0a1cd11728b29e0.jpg"),
                Fruit("猕猴桃","https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1523458728504&di=16e8c7913f0a535d5518c6585125f1b7&imgtype=0&src=http%3A%2F%2Fimg13.360buyimg.com%2Fn0%2Fjfs%2Ft151%2F271%2F2791567759%2F234300%2F2a2eed47%2F53d8a836N3587a408.jpg"),
                Fruit("山竹","https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1523458728502&di=7f2b62395f2c4823077f82867fec3a04&imgtype=0&src=http%3A%2F%2Fimgsrc.baidu.com%2Fimgad%2Fpic%2Fitem%2F18d8bc3eb13533fa6df010b3a2d3fd1f40345bed.jpg"),
                Fruit("杨桃","https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1523458728502&di=aed48913c203ed24d6a953144195c33b&imgtype=0&src=http%3A%2F%2Fimgsrc.baidu.com%2Fimgad%2Fpic%2Fitem%2F8ad4b31c8701a18bcb4799ba942f07082938fe93.jpg"),
                Fruit("橙子","https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1523458728502&di=2a62253343edebd7b4dd2525884881c1&imgtype=0&src=http%3A%2F%2Fimgsrc.baidu.com%2Fimgad%2Fpic%2Fitem%2F5d6034a85edf8db1095a9b630323dd54564e7445.jpg"),
                Fruit("樱桃","https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1523458728501&di=4cc1fc99926b9adfe265fa0cb4dce756&imgtype=0&src=http%3A%2F%2F58pic.ooopic.com%2F58pic%2F19%2F93%2F48%2F25958PICIhW.jpg"),
                Fruit("红提","https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1523458728498&di=9b12695a5686a7da9eeeb697a5d7e0cf&imgtype=0&src=http%3A%2F%2Fpic11.photophoto.cn%2F20090526%2F0035035569331277_b.jpg"),
                Fruit("哈密瓜","https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1523458728496&di=0067de549afe1688b2dacd6fe8ca03c3&imgtype=0&src=http%3A%2F%2Fpic3.womai.com%2Fupload%2F601%2F603%2F358812%2F378408%2F495233%2F495236%2F605429%2F605429_0_pic500_8581.jpg"),
                Fruit("西红柿","https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1523458728493&di=11fe800e9beaff293aa50acffe43ab59&imgtype=0&src=http%3A%2F%2Fimgsrc.baidu.com%2Fimgad%2Fpic%2Fitem%2Fa8773912b31bb051aa6328b53c7adab44bede0ec.jpg"),
                Fruit("桃","https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1523458728525&di=f9545bc911001e43301174f34580e418&imgtype=0&src=http%3A%2F%2Fimgsrc.baidu.com%2Fimgad%2Fpic%2Fitem%2F48540923dd54564ec6cb942cb9de9c82d1584f7c.jpg"),
                Fruit("酸橙","https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1523458728525&di=fe234bb991beec56176fad50fdd9d4db&imgtype=0&src=http%3A%2F%2Fimgsrc.baidu.com%2Fimgad%2Fpic%2Fitem%2F0dd7912397dda1446ac2c2a0b8b7d0a20df486dd.jpg"),
                Fruit("柚子","https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1523458728523&di=5398cfc37c6ddeffbc4038af652a5fe3&imgtype=0&src=http%3A%2F%2Fpic1.womai.com%2Fupload%2F601%2F603%2F606%2F64306%2F280374%2F82800%2F599438%2F599438_0_pic500_9233.jpg"),
                Fruit("杨梅","https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1523458728519&di=b307718a035620978adb9522073d7649&imgtype=0&src=http%3A%2F%2Fimgsrc.baidu.com%2Fimgad%2Fpic%2Fitem%2F6c224f4a20a446237acf2eff9222720e0cf3d722.jpg"),
                Fruit("苹果","https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1524053203&di=1ab3aee7b5ad5be1d1f5250bc8bd4382&imgtype=jpg&er=1&src=http%3A%2F%2Fwww.photophoto.cn%2Fm6%2F023%2F024%2F0230240027.jpg"),
                Fruit("菠萝","https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2270171933,1269226305&fm=27&gp=0.jpg"),
                Fruit("荔枝","https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1523458516759&di=6ac76b7a1c277ee8c4e804d7c98593f7&imgtype=0&src=http%3A%2F%2Fimg.weixinyidu.com%2F151117%2Fbcc64cef.jpg"),
                Fruit("草莓","https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=1856427000,1788429908&fm=27&gp=0.jpg"),
                Fruit("石榴","https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=949315503,3283317595&fm=27&gp=0.jpg"),
                Fruit("西瓜","https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1523458728133&di=fc2a4f6a288d79050b75537229130350&imgtype=0&src=http%3A%2F%2Fimgsrc.baidu.com%2Fimgad%2Fpic%2Fitem%2Fc83d70cf3bc79f3db0debe22b0a1cd11728b29e0.jpg"),
                Fruit("猕猴桃","https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1523458728504&di=16e8c7913f0a535d5518c6585125f1b7&imgtype=0&src=http%3A%2F%2Fimg13.360buyimg.com%2Fn0%2Fjfs%2Ft151%2F271%2F2791567759%2F234300%2F2a2eed47%2F53d8a836N3587a408.jpg"),
                Fruit("山竹","https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1523458728502&di=7f2b62395f2c4823077f82867fec3a04&imgtype=0&src=http%3A%2F%2Fimgsrc.baidu.com%2Fimgad%2Fpic%2Fitem%2F18d8bc3eb13533fa6df010b3a2d3fd1f40345bed.jpg"),
                Fruit("杨桃","https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1523458728502&di=aed48913c203ed24d6a953144195c33b&imgtype=0&src=http%3A%2F%2Fimgsrc.baidu.com%2Fimgad%2Fpic%2Fitem%2F8ad4b31c8701a18bcb4799ba942f07082938fe93.jpg"),
                Fruit("橙子","https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1523458728502&di=2a62253343edebd7b4dd2525884881c1&imgtype=0&src=http%3A%2F%2Fimgsrc.baidu.com%2Fimgad%2Fpic%2Fitem%2F5d6034a85edf8db1095a9b630323dd54564e7445.jpg"),
                Fruit("樱桃","https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1523458728501&di=4cc1fc99926b9adfe265fa0cb4dce756&imgtype=0&src=http%3A%2F%2F58pic.ooopic.com%2F58pic%2F19%2F93%2F48%2F25958PICIhW.jpg"),
                Fruit("红提","https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1523458728498&di=9b12695a5686a7da9eeeb697a5d7e0cf&imgtype=0&src=http%3A%2F%2Fpic11.photophoto.cn%2F20090526%2F0035035569331277_b.jpg"),
                Fruit("哈密瓜","https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1523458728496&di=0067de549afe1688b2dacd6fe8ca03c3&imgtype=0&src=http%3A%2F%2Fpic3.womai.com%2Fupload%2F601%2F603%2F358812%2F378408%2F495233%2F495236%2F605429%2F605429_0_pic500_8581.jpg"),
                Fruit("西红柿","https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1523458728493&di=11fe800e9beaff293aa50acffe43ab59&imgtype=0&src=http%3A%2F%2Fimgsrc.baidu.com%2Fimgad%2Fpic%2Fitem%2Fa8773912b31bb051aa6328b53c7adab44bede0ec.jpg"),
                Fruit("桃","https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1523458728525&di=f9545bc911001e43301174f34580e418&imgtype=0&src=http%3A%2F%2Fimgsrc.baidu.com%2Fimgad%2Fpic%2Fitem%2F48540923dd54564ec6cb942cb9de9c82d1584f7c.jpg"),
                Fruit("酸橙","https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1523458728525&di=fe234bb991beec56176fad50fdd9d4db&imgtype=0&src=http%3A%2F%2Fimgsrc.baidu.com%2Fimgad%2Fpic%2Fitem%2F0dd7912397dda1446ac2c2a0b8b7d0a20df486dd.jpg"),
                Fruit("柚子","https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1523458728523&di=5398cfc37c6ddeffbc4038af652a5fe3&imgtype=0&src=http%3A%2F%2Fpic1.womai.com%2Fupload%2F601%2F603%2F606%2F64306%2F280374%2F82800%2F599438%2F599438_0_pic500_9233.jpg"),
                Fruit("杨梅","https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1523458728519&di=b307718a035620978adb9522073d7649&imgtype=0&src=http%3A%2F%2Fimgsrc.baidu.com%2Fimgad%2Fpic%2Fitem%2F6c224f4a20a446237acf2eff9222720e0cf3d722.jpg")
        )
    }

    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        menuInflater.inflate(R.menu.toobar, menu)
        return true
    }

    override fun onOptionsItemSelected(item: MenuItem?): Boolean {
        when (item?.itemId) {
            R.id.backUp -> toast("backup")
            R.id.delete -> toast("delete")
            R.id.settings -> toast("settings")
            android.R.id.home-> drawerLayout.openDrawer(GravityCompat.START)
        }
        return true
    }
}
